package com.truyentranh.modules.news;

import it.sephiroth.android.library.imagezoom.graphics.FastBitmapDrawable;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.truyentranh.R;
import com.truyentranh.data.NewsDataObject;
import com.truyentranh.modules.cabvn.CabVnActivity;
import com.truyentranh.network.APIRequestWorker;
import com.truyentranh.network.Request;
import com.truyentranh.network.RequestListener;
import com.truyentranh.requester.NewsRequester;
import com.truyentranh.requester.RssRequester;
import com.truyentranh.utils.AppSettingParams;
import com.truyentranh.utils.IImageCached;
import com.truyentranh.utils.Logger;

public class NewsActivity extends Activity {

	LinearLayout loadingpage;
	LinearLayout tblLayout;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		//rem clear resource
		// Remove notification bar
		this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
//		setContentView(R.layout.testact);
//
//		loadingpage = (LinearLayout) findViewById(R.id.loadingpage);
//		tblLayout = (LinearLayout) findViewById(R.id.tblLayout);

	}

	boolean isGettingData = false;

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		if (!isGettingData) {
			isGettingData = true;
			initNewsData();
		}
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		// initNewsData();
	}

	void initNewLayout(final ArrayList<NewsDataObject> newsDataObjects) {
		//rem clear resource
//		final LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//		if (newsDataObjects != null) {
//
//			runOnUiThread(new Runnable() {
//
//				public void run() {
//					tblLayout.setVisibility(View.VISIBLE);
//					loadingpage.setVisibility(View.GONE);
//
//					final int numCol = 3;
//					final int numRow = newsDataObjects.size() / numCol;
//
//					for (int i = 0; i < numRow; i++) {
//
//						FrameLayout rowHeader = (FrameLayout) inflater.inflate(R.layout.shelf_item, tblLayout, false);
//						TextView titleNewsText = (TextView) rowHeader.findViewById(R.id.titleNewsText);
//						titleNewsText.setVisibility(View.GONE);
//						LinearLayout line = (LinearLayout) rowHeader.findViewById(R.id.tblLayoutLine);
//
//						for (int j = 0; j < numCol; j++) {
//							final LinearLayout hoderNewsImage = (LinearLayout) inflater.inflate(R.layout.news_render_item, rowHeader, false);
//							final Button imageView = (Button) hoderNewsImage.findViewById(R.id.mainImage);
//
//							if (imageView.getParent() != null) {
//								((ViewGroup) imageView.getParent()).removeAllViews();
//							}
//							NewsDataObject dataObject = newsDataObjects.get(i * numCol + j);							
//							imageView.setId(dataObject.getId());
//							imageView.setOnClickListener(onClickListener);
//							line.addView(imageView);
//						}
//
//						tblLayout.addView(rowHeader, i);
//					}
//				}
//			});
//		}
	}

	OnClickListener onClickListener = new OnClickListener() {

		public void onClick(View v) {
			if (isMySelf) {
				Intent intent = new Intent(getApplication(), RssViewActivity.class);
				intent.putExtra("NEW_ID", v.getId());
				startActivity(intent);

				// RssRequester rssRequester = new RssRequester(v.getId(), new
				// RequestListener() {
				//
				// public void onRequestFailed(String errorMsg, Request request)
				// {
				//
				//
				// }
				//
				// public void onRequestComplete(Request request) {
				//
				//
				// }
				// });
				// requestId = AppSettingParams.nextRequestId();
				// rssRequester.setRequestId(requestId);
				// APIRequestWorker.getInstance().addAPIRequest(rssRequester);
			} else {
				Logger.logMe(">>>>>>>>>>> request data");
			}
		}
	};

	long requestId = -1;
	boolean isMySelf = false;

	void initNewsData() {
		NewsRequester newsRequester = new NewsRequester(new RequestListener() {

			public void onRequestFailed(String errorMsg, Request request) {
				Logger.logMe("errorMsg = " + errorMsg);
			}

			public void onRequestComplete(Request request) {

				if (request instanceof NewsRequester) {
					NewsRequester r = (NewsRequester) request;
					if (r.getArrayList() != null) {
						initNewLayout(r.getArrayList());
					}
					isMySelf = r.isMySelf();
				}
			}
		});
		requestId = AppSettingParams.nextRequestId();
		newsRequester.setRequestId(requestId);
		APIRequestWorker.getInstance().addAPIRequest(newsRequester);
	}

}
